﻿#include "widget.h"
#include <QApplication>
#include <QTextCodec>
#include <QDebug>
#include <iostream>
#include <QMap>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);

    QApplication a(argc, argv);

    Widget w;


    if(argc>1){
        QString appName;
//        char* oprArg;
        int msec = 5000;
        QString code;
        QString path;
        QStringList listInfo;
        listInfo.append("        _______        _    __  __                   ");
        listInfo.append("       |__   __|      | |  |  \\/  |                  ");
        listInfo.append("          | | __ _ ___| | _| \\  / | __ _ _ __        ");
        listInfo.append("          | |/ _` / __| |/ | |\\/| |/ _` | '_ \\       ");
        listInfo.append("          | | (_| \\__ |   <| |  | | (_| | | | |      ");
        listInfo.append("        _ |_|\\__,_|_____|\\_|_|  |_|\\__,_|_| |_|      ");
        listInfo.append("       | |           | |   | |  | |                  ");
        listInfo.append("       | |__  _   _  | |__ | | _| | ___   ___   ___  ");
        listInfo.append("       | '_ \\| | | | | '_ \\| |/ | |/ _ \\ / _ \\ / _ \\ ");
        listInfo.append("       | |_) | |_| |_| |_) |   <| | (_) | (_) | (_) |");
        listInfo.append("       |_.__/ \\__, (_|_.__/|_|\\_|_|\\___/ \\___/ \\___/ ");
        listInfo.append("               __/ |                                 ");
        listInfo.append("              |___/                                  ");

        for(int i=0;i<listInfo.length();i++){
            cout << listInfo.at(i).toStdString() << endl;
        }

        for(int i=1;i<argc;i++){
            if(strstr(argv[i], "-h")){
                cout << "TaskMan Version 1.0.1" << endl;
                cout << "TASKMAN [-h] [-n] [-p] [-t] [-c]" << endl;
                cout << "[-h]\t" << "Display help menu" << endl;
                cout << "[-n] AppName\t" << "Add program name(optional),"
                                            "When this option is added, this program name defaults to the process name" << endl;
                cout << "[-p] Path\t" << "Add the main program path,You have to enter the path" << endl;
                cout << "[-t] MonitorTime(ms)\t" << "Add a monitoring interval, Default is 5000ms" << endl;
                cout << "[-c] code\t" << "Set program code(only UTF-8 GBK or GB2312),Default is UTF-8" << endl;

            }
            else if(strstr(argv[i], "-n")){
                appName = argv[i+1];
            }
            else if(strstr(argv[i], "-p")){
                path = argv[i+1];
            }
            else if(strstr(argv[i], "-t")){
                msec = atoi(argv[i+1]);
            }
            else if(strstr(argv[i], "-c")){
                code = argv[i+1];
            }
        }

        if(path!=""){
            QMap<QString, QString> mapData;
            mapData.insert("AppName", appName);
            mapData.insert("path", path);
            mapData.insert("msec", QString::number(msec));
            mapData.insert("code", code);

            w.cmdCtrl(mapData);
        }
        cout << "AppName:\t" << appName.toStdString() << endl;
        cout << "path:\t" << path.toStdString() << endl;
        cout << "MonitorTime(ms):\t" << QString::number(msec).toStdString()  << endl;
        cout << "code:\t" << code.toStdString() << endl;

    }
    else {
        //隐藏控制台窗口 win32api
        FreeConsole();
        w.show();
    }
    return a.exec();
}
